using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ActiproSoftware.Windows.Controls.Docking;
using ActiproSoftware.Windows.Media;

namespace AppDemo
{

    /// <summary>
    /// Interaction logic for DockingTabbedMdiWindow1.xaml.
    /// </summary>
    public partial class DockingTabbedMdiWindow1 : Window
    {

        public DockingTabbedMdiWindow1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OnLoaded);
          //  DockSite.SetControlSize(dockedBottom, new Size(400, 200));
        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // NON-PUBLIC PROCEDURES
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Occurs when the <c>Layout.EvenlyDistributeDocumentsOnly</c> menu item is clicked.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private void OnLayoutEvenlyDistributeDocumentsOnlyMenuItemClick(object sender, RoutedEventArgs e)
        {
            Workspace workspace = this.dockSite.Workspace;
            if (null != workspace)
            {
                IList<DependencyObject> descendents = VisualTreeHelperExtended.GetAllDescendants(workspace, typeof(SplitContainer));
                if (null != descendents)
                {
                    foreach (SplitContainer splitContainer in descendents)
                        splitContainer.ResizeSlots();
                }
            }
        }

        /// <summary>
        /// Occurs when the <c>Layout.EvenlyDistributeFavorFocused</c> menu item is clicked.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private void OnLayoutEvenlyDistributeFavorFocusedMenuItemClick(object sender, RoutedEventArgs e)
        {
            IList<DependencyObject> descendents = VisualTreeHelperExtended.GetAllDescendants(this.dockSite, typeof(SplitContainer));
            if (null != descendents)
            {
                foreach (SplitContainer splitContainer in descendents)
                {

                    // Look for SplitContainers that contain the focused element, and increase the ratios for that slot
                    int visualCount = VisualTreeHelper.GetChildrenCount(splitContainer);
                    double[] desiredRatios = new double[visualCount];
                    for (int i = 0, visibleChildCount = 0; i < visualCount; i++)
                    {
                        // Default ratio, must also ensure that we don't pass a ratio that is less than or equal to 0
                        desiredRatios[i] = 1;

                        // Get the child and verify that it is visible
                        FrameworkElement child = VisualTreeHelper.GetChild(splitContainer, i) as FrameworkElement;
                        if (null != child && child.IsVisible)
                        {
                            // If the child has the keyboard focus, then increase it's ratio
                            if (child.IsKeyboardFocusWithin)
                                desiredRatios[visibleChildCount] = 3;
                            visibleChildCount++;
                        }
                    }

                    splitContainer.ResizeSlots(desiredRatios);
                }
            }
        }

        /// <summary>
        /// Occurs when the <c>Layout.EvenlyDistributeFavorWorkspace</c> menu item is clicked.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private void OnLayoutEvenlyDistributeFavorWorkspaceMenuItemClick(object sender, RoutedEventArgs e)
        {
            IList<DependencyObject> descendents = VisualTreeHelperExtended.GetAllDescendants(this.dockSite, typeof(SplitContainer));
            if (null != descendents)
            {
                foreach (SplitContainer splitContainer in descendents)
                {

                    // Look for SplitContainers that contain the Workspace, and increase the ratios for that slot
                    int visualCount = VisualTreeHelper.GetChildrenCount(splitContainer);
                    double[] desiredRatios = new double[visualCount];
                    for (int i = 0, visibleChildCount = 0; i < visualCount; i++)
                    {
                        // Default ratio, must also ensure that we don't pass a ratio that is less than or equal to 0
                        desiredRatios[i] = 1;

                        // Get the child and verify that it is visible
                        FrameworkElement child = VisualTreeHelper.GetChild(splitContainer, i) as FrameworkElement;
                        if (null != child && child.IsVisible)
                        {
                            // If the child is a Workspace, or contains the Workspace, then increase it's ratio
                            if (child is Workspace || null != VisualTreeHelperExtended.GetFirstDescendant(child, typeof(Workspace)))
                                desiredRatios[visibleChildCount] = 3;
                            visibleChildCount++;
                        }
                    }

                    splitContainer.ResizeSlots(desiredRatios);
                }
            }
        }

        /// <summary>
        /// Occurs when the <c>Layout.EvenlyDistributeToolsOnly</c> menu item is clicked.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private void OnLayoutEvenlyDistributeToolsOnlyMenuItemClick(object sender, RoutedEventArgs e)
        {
            IList<DependencyObject> descendents = VisualTreeHelperExtended.GetAllDescendants(this.dockSite, typeof(SplitContainer));
            if (null != descendents)
            {
                foreach (SplitContainer splitContainer in descendents)
                {
                    if (null != VisualTreeHelperExtended.GetAncestor(splitContainer, typeof(Workspace)))
                        continue;

                    splitContainer.ResizeSlots();
                }
            }
        }

        /// <summary>
        /// Occurs when the <c>Layout.EvenlyDistribute</c> menu item is clicked.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private void OnLayoutEvenlyDistributeMenuItemClick(object sender, RoutedEventArgs e)
        {
            IList<DependencyObject> descendents = VisualTreeHelperExtended.GetAllDescendants(this.dockSite, typeof(SplitContainer));
            if (null != descendents)
            {
                foreach (SplitContainer splitContainer in descendents)
                    splitContainer.ResizeSlots();
            }
        }

        /// <summary>
        /// Occurs when the <c>Layout.RandomlyDistribute</c> menu item is clicked.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private void OnLayoutRandomlyDistributeMenuItemClick(object sender, RoutedEventArgs e)
        {
            IList<DependencyObject> descendents = VisualTreeHelperExtended.GetAllDescendants(this.dockSite, typeof(SplitContainer));
            if (null != descendents)
            {
                Random random = new Random();
                foreach (SplitContainer splitContainer in descendents)
                {
                    splitContainer.ResizeSlots(null);
                }
            }
        }

        /// <summary>
        /// Occurs when the sample is loaded.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Start off random
            this.OnLayoutRandomlyDistributeMenuItemClick(null, null);
        }

    }
}
